<template>
    <div class="login-container">
        <!-- 添加渐变背景遮罩 -->
        <div class="background-overlay"></div>
        <div class="login-content">
            <div class="top">
                <!-- 使用 Vant 4 的 Image 组件并添加动画 -->
                <van-image class="logo animated-logo" src="/src/assets/ico.png" round fit="cover" />
            </div>
            <!-- 使用 Vant 4 的 Form 组件并添加阴影 -->
            <van-form @submit="onSubmit" class="login-form">
                <van-field
                    v-model="username"
                    name="username"
                    label="用户名"
                    placeholder="请输入用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]"
                    class="field-item"
                />
                <van-field
                    v-model="password"
                    type="password"
                    name="password"
                    label="密码"
                    placeholder="请输入密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
                    class="field-item"
                />
            </van-form>
            <van-button round block type="primary" native-type="submit" @click="onSubmit" class="login-button" color="var(--gradient-primary)">
                登录
            </van-button>
            <div class="bottom">
                <div class="register">还没有账号？<span class="protocol" @click="goRegister">点击注册</span></div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import { UserControllerService } from '../../api/services/UserControllerService';

const router = useRouter();
const username = ref('');
const password = ref('');

const onSubmit = async () => {
    try {
        const res = await UserControllerService.doLogin({
            username: username.value,
            password: password.value
        });
        console.log(res);
        if (res.code === 0) {
            showToast('登录成功');
            localStorage.setItem('token', res.data);
            localStorage.setItem('username', username.value);
            router.push({ name: "Home" });
        }
    } catch (error) {
        showToast('登录失败');
        console.error('登录错误:', error);
    }
};

const goRegister = () => {
    router.push({ name: "Register" });
}
</script>

<style scoped>
.login-container {
    padding: 20px;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.background-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.8); /* 半透明遮罩 */
    z-index: -1;
}

.login-content {
    width: 100%;
    max-width: 400px;
    padding: 40px;
    background: white;
    border-radius: 16px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.top {
    display: flex;
    justify-content: center;
    margin-bottom: 40px;
}

.animated-logo {
    width: 120px;
    height: 120px;
    animation: fadeIn 1s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.login-form {
    margin-bottom: 30px;
}

.field-item {
    margin-bottom: 20px;
}

.login-button {
    margin-top: 30px;
    background: linear-gradient(45deg, #007bff, #00c6ff); /* 渐变按钮 */
    border: none;
    transition: transform 0.2s;
}

.login-button:active {
    transform: scale(0.98);
}

.bottom {
    display: flex;
    justify-content: center;
    margin-top: 20px;
}

.register {
    color: #666;
    font-size: 14px;
}

.protocol {
    color: #007bff;
    text-decoration: underline;
    cursor: pointer;
}
</style>